package main

import (
	"context"
	"fmt"
	pb "go_add/proto"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

const (
	port     = ":50051"
	unixPath = "/tmp/go-add.sock"
)

func main() {
	// conn, err := grpc.Dial("127.0.0.1"+port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	conn, err := grpc.Dial(unixPath, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
		unixAddress, err := net.ResolveUnixAddr("unix", addr)
		if err != nil {
			return nil, err
		}
		return net.DialUnix("unix", nil, unixAddress)
	}))
	if err != nil {
		panic(err)
	}
	defer func(conn *grpc.ClientConn) {
		_ = conn.Close()
	}(conn)
	c := pb.NewAddClient(conn)
	r, err := c.Add(context.Background(), &pb.AddRequest{
		A: 4,
		B: 5,
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%d\n", r.GetMessage())
}
